#include "types.h"
#include "util_buffer.h"

void util_buffer_init(Buffer *buffer, U32 length, U8 *dataArray)
{
	buffer->length = length;
	buffer->dataArray = dataArray;
	util_buffer_clear(buffer);
}

void util_buffer_clear(Buffer *buffer)
{
	buffer->writingPointer = 0;
	buffer->readingPointer = 0;
	buffer->size = 0;
}

BOOL util_buffer_insert(Buffer *buffer, U8 byte)
{
	if(buffer->size < buffer->length)
	{
		buffer->dataArray[buffer->writingPointer++] = byte;

		if(buffer->writingPointer == buffer->length){
			buffer->writingPointer = 0;
		}

		buffer->size++;

		return TRUE;
	}
	else{
		return FALSE;
	}
}

BOOL util_buffer_remove(Buffer *buffer, U8 *byte)
{
	if(buffer->size > 0)
	{
		*byte = buffer->dataArray[buffer->readingPointer];

		buffer->readingPointer++;

		if(buffer->readingPointer == buffer->length)
		{
			buffer->readingPointer = 0;
		}

		buffer->size--;

		return TRUE;
	}
	else
	{
		return FALSE;
	}
}
